package com.yjq.programmer.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yjq.programmer.bean.CodeMsg;
import com.yjq.programmer.dao.PosterMapper;
import com.yjq.programmer.domain.Poster;
import com.yjq.programmer.domain.PosterExample;
import com.yjq.programmer.dto.PageDTO;
import com.yjq.programmer.dto.PosterDTO;
import com.yjq.programmer.dto.ResponseDTO;
import com.yjq.programmer.service.IPosterService;
import com.yjq.programmer.utils.CommonUtil;
import com.yjq.programmer.utils.CopyUtil;
import com.yjq.programmer.utils.UuidUtil;
import com.yjq.programmer.utils.ValidateEntityUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 杨杨吖
 * @QQ 823208782
 * @WX yjqi12345678
 * @create 2022-11-09 9:04
 */
@Service
@Transactional
public class PosterServiceImpl implements IPosterService {


    @Resource
    private PosterMapper posterMapper;

    /**
     * 分页获取海报数据
     * @param pageDTO
     * @return
     */
    @Override
    public ResponseDTO<PageDTO<PosterDTO>> getPosterListByPage(PageDTO<PosterDTO> pageDTO) {
        PosterExample posterExample = new PosterExample();
        // 不知道当前页多少，默认为第一页
        if(pageDTO.getPage() == null){
            pageDTO.setPage(1);
        }
        if(pageDTO.getSize() == null) {
            pageDTO.setSize(5);
        }
        posterExample.setOrderByClause("sort desc, id asc");
        PageHelper.startPage(pageDTO.getPage(), pageDTO.getSize());
        // 分页查出海报数据
        List<Poster> posterList = posterMapper.selectByExample(posterExample);
        PageInfo<Poster> pageInfo = new PageInfo<>(posterList);
        // 获取数据的总数
        pageDTO.setTotal(pageInfo.getTotal());
        // 将domain类型数据  转成 DTO类型数据
        List<PosterDTO> posterDTOList = CopyUtil.copyList(posterList, PosterDTO.class);
        pageDTO.setList(posterDTOList);
        return ResponseDTO.success(pageDTO);
    }

    /**
     * 保存海报数据(添加、修改)
     * @param posterDTO
     * @return
     */
    @Override
    public ResponseDTO<Boolean> savePoster(PosterDTO posterDTO) {
        // 进行统一表单验证
        CodeMsg validate = ValidateEntityUtil.validate(posterDTO);
        if(!validate.getCode().equals(CodeMsg.SUCCESS.getCode())){
            return ResponseDTO.errorByMsg(validate);
        }
        Poster poster = CopyUtil.copy(posterDTO, Poster.class);
        if(CommonUtil.isEmpty(poster.getId())){
            // id为空 说明是添加数据
            // 最多添加五张海报
            List<Poster> posterList = posterMapper.selectByExample(new PosterExample());
            if(posterList.size() >= 5) {
                return ResponseDTO.errorByMsg(CodeMsg.POSTER_OVER_MAX);
            }
            // 生成8位id
            poster.setId(UuidUtil.getShortUuid());
            // 添加数据到数据库
            if(posterMapper.insertSelective(poster) == 0){
                return ResponseDTO.errorByMsg(CodeMsg.POSTER_ADD_ERROR);
            }
        }else{
            // id不为空 说明是修改数据
            // 修改数据库中数据
            if(posterMapper.updateByPrimaryKeySelective(poster) == 0){
                return ResponseDTO.errorByMsg(CodeMsg.POSTER_EDIT_ERROR);
            }
        }
        return ResponseDTO.successByMsg(true, "保存成功！");
    }

    /**
     * 删除海报数据
     * @param posterDTO
     * @return
     */
    @Override
    public ResponseDTO<Boolean> removePoster(PosterDTO posterDTO) {
        if(CommonUtil.isEmpty(posterDTO.getId())){
            return ResponseDTO.errorByMsg(CodeMsg.DATA_ERROR);
        }
        // 删除海报信息
        if(posterMapper.deleteByPrimaryKey(posterDTO.getId()) == 0){
            return ResponseDTO.errorByMsg(CodeMsg.POSTER_DELETE_ERROR);
        }
        return ResponseDTO.successByMsg(true, "删除成功！");
    }

    /**
     * 小程序获取海报数据
     * @return
     */
    @Override
    public ResponseDTO<List<PosterDTO>> getPosterList() {
        PosterExample posterExample = new PosterExample();
        posterExample.setOrderByClause("sort desc");
        List<Poster> posterList = posterMapper.selectByExample(posterExample);
        return ResponseDTO.success(CopyUtil.copyList(posterList, PosterDTO.class));
    }
}
